#!/usr/bin/env python3
# coding: utf-8
"""
2024-02-18 脚本管理窗口
"""

from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QMessageBox, QLineEdit, QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView
from PySide6.QtCore import Qt
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

from top.wardyan.core.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow
from wmain import *
from top.wardyan.core.vo.vos import Record



class WScriptWindow (QWidget) :
    # def __init__(self, parent: Optional[PySide6.QtWidgets.QWidget] = ..., f: PySide6.QtCore.Qt.WindowType = ...) -> None:
    #     super().__init__(parent, f)

    table : QTableWidget = None

    def __init__(self) -> None:
        super().__init__()
        self.main()

        data : list[Record] = []
        r : Record = Record()
        r.set("name", "n1")
        r.set("age", "2")
        r.set("school", "??")
        data.append(r)
        data.append(r)
        data.append(r)
        data.append(r)
        data.append(r)
        self.renderTable(data)

    def renderTable(self, data : list[Record]) -> None :
        """刷新窗口数据

        Args:
            data (_type_): _description_
        table.setRowCount(10)
        table.setColumnCount(3)
        ## table.setColumnCount()
        
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
        for i in range(0, 4) :
            for j in range(0, 2) :
                item : QTableWidgetItem = QTableWidgetItem(str(i) + "-" + str(j))
                ## item.setFlags(Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsSelectable)
                #item.setFlags(Qt.ItemFlag.ItemIsEditable )
                table.setItem(i,j,item)
        """
        if data is None :
            data = []
        rowcnt = len(data)
        colcnt = 3
        self.table.setRowCount(rowcnt)
        ## 列数和列名取自配置文件
        self.table.setColumnCount(colcnt)
        ##

        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        self.table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
        for i in range(0, rowcnt) :
            r : Record = data[i]
            for j in range(0, len(r.keys())) :
                s : str = r.getIdx(j)
                item : QTableWidgetItem = QTableWidgetItem(s)
                ## item.setFlags(Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsSelectable)
                #item.setFlags(Qt.ItemFlag.ItemIsEditable )
                self.table.setItem(i,j,item)
        


    def main(self) :
        HEIGHT = 30
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)
        

        ## 按钮: 搜索 脚本清单 新增脚本 表/视图结构
        # btn : QPushButton = QPushButton("TESTING")
        # btn.setMinimumHeight(50)
        # ## btn.clicked.connect(button_click_script)
        # layout.addWidget(btn)
        ctrlw : QWidget = QWidget()
        ctrllayout : QHBoxLayout = QHBoxLayout()
        ctrlw.setLayout(ctrllayout)
        layout.addWidget(ctrlw)


        ctrllayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        
        txtSearch : QLineEdit = QLineEdit("")
        txtSearch.setFixedWidth(HEIGHT * 5)
        txtSearch.setFixedHeight(HEIGHT)
        txtSearch.setMaxLength(50)
        ## 要设置输入框自动获得焦点 暂时先不搞, 暂时搞不定
        # txtSearch.setModified(True)
        # txtSearch.setFocus()
        
        ctrllayout.addWidget(txtSearch)

        btn : QPushButton = QPushButton("脚本清单")
        btn.setFixedHeight(HEIGHT)
        ## btn.clicked.connect(button_click_)
        btn.setFixedWidth(HEIGHT * 3)
        ctrllayout.addWidget(btn)

        btn : QPushButton = QPushButton("新增脚本")
        btn.setFixedHeight(HEIGHT)
        ## btn.clicked.connect(button_click_)
        btn.setFixedWidth(HEIGHT * 3)
        ctrllayout.addWidget(btn)

        btn : QPushButton = QPushButton("表/视图结构")
        btn.setFixedHeight(HEIGHT)
        ## btn.clicked.connect(button_click_)
        btn.setFixedWidth(HEIGHT * 3)
        ctrllayout.addWidget(btn)

        ctrlt : QWidget = QWidget()
        tbllayout : QHBoxLayout = QHBoxLayout()
        ctrlt.setLayout(tbllayout)
        layout.addWidget(ctrlt)

        self.table = QTableWidget()
        tbllayout.addWidget(self.table)
        """
        table.setRowCount(10)
        table.setColumnCount(3)
        ## table.setColumnCount()
        
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
        for i in range(0, 4) :
            for j in range(0, 2) :
                item : QTableWidgetItem = QTableWidgetItem(str(i) + "-" + str(j))
                ## item.setFlags(Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsSelectable)
                #item.setFlags(Qt.ItemFlag.ItemIsEditable )
                table.setItem(i,j,item)

        """
        ##txtSearch.setFocus()
        ## txtSearch.focus
        ## layout.addWidget(ctrllayout)
        ## layout.addChildLayout(ctrllayout)
        
        ##menuBar.setMinimumHeight(HEIGHT)
        ##self.setMenuWidget(menuBar)
        

if __name__ == "__main__" :     ## or 1 == 1
    app = QApplication([])
    w = WDevClientMainWindow()
    # ## w = WScriptWindow()
    w.show()
    app.exec()